home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / slzwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.4 KB  |  406 lines

  1. /* Copyright (C) 1993, 1996, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: slzwd.c,v 1.2 2000/09/19 19:00:50 lpd Exp $ */
  20. /* LZW decoding filter */
  21. #include "stdio_.h"        /* includes std.h */
  22. #include "gdebug.h"
  23. #include "strimpl.h"
  24. #include "slzwx.h"
  25.  
  26. /* ------ LZWDecode ------ */
  27.  
  28. /********************************************************/
  29. /* LZW routines are based on:                           */
  30. /* Dr. Dobbs Journal --- Oct. 1989.                     */
  31. /* Article on LZW Data Compression by Mark R. Nelson    */
  32. /********************************************************/
  33.  
  34. /* Define the special codes in terms of code_escape, which is */
  35. /* 1 << InitialCodeLength. */
  36. #define code_reset (code_escape + 0)
  37. #define code_eod (code_escape + 1)
  38. #define code_0 (code_escape + 2)    /* first assignable code */
  39.  
  40. struct lzw_decode_s {
  41.     byte datum;
  42.     byte len;            /* length of code */
  43.     ushort prefix;        /* code to be prefixed */
  44. };
  45.  
  46. gs_private_st_simple(st_lzw_decode, lzw_decode, "lzw_decode");
  47. /* We can use a simple type as the element type, */
  48. /* because there are no pointers to enumerate or relocate. */
  49. #define st_lzw_decode_element st_lzw_decode
  50. #define lzw_decode_max 4096    /* must be 4096 */
  51.  
  52. /* Initialize LZWDecode filter */
  53. /* We separate out the reset function for some non-stream clients. */
  54. private int
  55. s_LZWD_reset(stream_state * st)
  56. {
  57.     stream_LZW_state *const ss = (stream_LZW_state *) st;
  58.     register lzw_decode *dc = ss->table.decode;
  59.     register int i;
  60.     uint code_escape = 1 << ss->InitialCodeLength;
  61.  
  62.     ss->bits_left = 0;
  63.     ss->bytes_left = 0;
  64.     ss->next_code = code_0;
  65.     ss->code_size = ss->InitialCodeLength + 1;
  66.     ss->prev_code = -1;
  67.     ss->copy_code = -1;
  68.     dc[code_reset].len = 255;
  69.     dc[code_eod].len = 255;
  70.     for (i = 0; i < code_escape; i++, dc++)
  71.     dc->datum = i, dc->len = 1, dc->prefix = code_eod;
  72.     return 0;
  73. }
  74. private int
  75. s_LZWD_init(stream_state * st)
  76. {
  77.     stream_LZW_state *const ss = (stream_LZW_state *) st;
  78.     lzw_decode *dc =
  79.     gs_alloc_struct_array(st->memory, lzw_decode_max + 1,
  80.               lzw_decode, &st_lzw_decode_element,
  81.               "LZWDecode(init)");
  82.  
  83.     if (dc == 0)
  84.     return ERRC;
  85. /****** WRONG ******/
  86.     ss->table.decode = dc;
  87.     return s_LZWD_reset(st);
  88. }
  89.  
  90. /* Process a buffer */
  91. private int
  92. s_LZWD_process(stream_state * st, stream_cursor_read * pr,
  93.            stream_cursor_write * pw, bool last)
  94. {
  95.     stream_LZW_state *const ss = (stream_LZW_state *) st;
  96.     register const byte *p = pr->ptr;
  97.     register byte *q = pw->ptr;
  98.  
  99. #ifdef DEBUG
  100.     byte *q0 = q;
  101.  
  102. #endif
  103.     const byte *rlimit = pr->limit;    /* constant pointer */
  104.     byte *wlimit = pw->limit;    /* constant pointer */
  105.     int status = 0;
  106.     int code = ss->copy_code;
  107.     int prev_code = ss->prev_code;
  108.     uint prev_len = ss->prev_len;
  109.     byte bits = ss->bits;
  110.     int bits_left = ss->bits_left;
  111.     int bytes_left = ss->bytes_left;
  112.     int code_size = ss->code_size;
  113.     int code_mask;
  114.     int switch_code;
  115.     int next_code = ss->next_code;
  116.     lzw_decode *table = ss->table.decode;    /* constant pointer */
  117.     lzw_decode *dc_next = table + next_code;    /* invariant */
  118.     lzw_decode *dc;
  119.     int code_escape = 1 << ss->InitialCodeLength;
  120.     int eod = code_eod;
  121.     bool low_order = ss->FirstBitLowOrder;
  122.     uint len;
  123.     int c;
  124.     byte b;
  125.     byte *q1;
  126.  
  127.     if_debug2('w', "[w]process decode: code_size=%d next_code=%d\n",
  128.           code_size, next_code);
  129. #define set_code_size()\
  130.   code_mask = (1 << code_size) - 1,\
  131.   switch_code = code_mask + 1 - ss->EarlyChange
  132.     set_code_size();
  133.     if (!ss->BlockData)
  134.     bytes_left = rlimit - p + 2;    /* never stop for bytes_left */
  135.     /* If we are in the middle of copying a string, */
  136.     /* do some more now. */
  137.     if (code >= 0) {
  138.     int rlen = ss->copy_left;
  139.     int wlen = wlimit - q;
  140.     int n = len = min(rlen, wlen);
  141.  
  142.     c = code;
  143.     ss->copy_left = rlen -= len;
  144.     if_debug3('W', "[W]copying 0x%x, %d byte(s) out of %d left\n",
  145.           code, len, rlen + len);
  146.     while (rlen)
  147.         c = table[c].prefix,
  148.         rlen--;
  149.     q1 = q += len;
  150.     n = len;
  151.     while (--n >= 0) {
  152.         *q1-- = (dc = &table[c])->datum;
  153.         c = dc->prefix;
  154.     }
  155.     if (ss->copy_left) {    /* more to do */
  156.         pw->ptr = q;
  157.         return 1;
  158.     }
  159.     ss->copy_code = -1;
  160.     len = ss->copy_len;
  161.     /* Retrieve the first byte of the code just copied. */
  162.     if (c == eod) {        /* We just copied the entire code, */
  163.         /* so the byte we want is immediately available. */
  164.         b = q1[1];
  165.     } else {        /* We have to scan to the beginning of the code. */
  166.         for (; c != eod; c = table[c].prefix)
  167.         b = (byte) c;
  168.     }
  169.     goto add;
  170.     }
  171.   top:if (code_size > bits_left) {
  172.     if (bytes_left == 0) {
  173.         if (p == rlimit)
  174.         goto out;
  175.         bytes_left = *++p;
  176.         if_debug1('W', "[W]block count %d\n", bytes_left);
  177.         if (bytes_left == 0) {
  178.         status = EOFC;
  179.         goto out;
  180.         }
  181.         goto top;
  182.     }
  183.     if (low_order)
  184.         code = bits >> (8 - bits_left);
  185.     else
  186.         code = (uint) bits << (code_size - bits_left);
  187.     if (bits_left + 8 < code_size) {    /* Need 2 more data bytes */
  188.         if (bytes_left == 1) {
  189.         if (rlimit - p < 3)
  190.             goto out;
  191.         bytes_left = p[2];
  192.         if_debug1('W', "[W]block count %d\n",
  193.               bytes_left);
  194.         if (bytes_left == 0) {
  195.             status = EOFC;
  196.             goto out;
  197.         }
  198.         bytes_left++;
  199.         bits = p[1];
  200.         p++;
  201.         } else {
  202.         if (rlimit - p < 2)
  203.             goto out;
  204.         bits = p[1];
  205.         }
  206.         if (low_order)
  207.         code += (uint) bits << bits_left;
  208.         else
  209.         code += (uint) bits << (code_size - 8 - bits_left);
  210.         bits_left += 8;
  211.         bits = p[2];
  212.         p += 2;
  213.         bytes_left -= 2;
  214.     } else {
  215.         if (p == rlimit)
  216.         goto out;
  217.         bits = *++p;
  218.         bytes_left--;
  219.     }
  220.     if (low_order)
  221.         code += (uint) bits << bits_left,
  222.         bits_left += 8 - code_size;
  223.     else
  224.         bits_left += 8 - code_size,
  225.         code += bits >> bits_left;
  226.     } else {
  227.     if (low_order)
  228.         code = bits >> (8 - bits_left),
  229.         bits_left -= code_size;
  230.     else
  231.         bits_left -= code_size,
  232.         code = bits >> bits_left;
  233.     }
  234.     code &= code_mask;
  235.     if_debug2('W', "[W]reading 0x%x,%d\n", code, code_size);
  236.     /*
  237.      * There is an anomalous case where a code S is followed
  238.      * immediately by another occurrence of the S string.
  239.      * In this case, the next available code will be defined as
  240.      * S followed by the first character of S, and will be
  241.      * emitted immediately after the code S.  We have to
  242.      * recognize this case specially, by noting that the code is
  243.      * equal to next_code.
  244.      */
  245.     if (code >= next_code) {
  246.     if (code > next_code) {
  247. #ifdef DEBUG
  248.         lprintf2("[W]code = %d > next_code = %d\n",
  249.              code, next_code);
  250. #endif
  251.         status = ERRC;
  252.         goto out;
  253.     }
  254.     /* Fabricate the entry for the code.  It will be */
  255.     /* overwritten immediately, of course. */
  256.     for (c = prev_code; c != eod; c = table[c].prefix)
  257.         dc_next->datum = c;
  258.     len = prev_len + 1;
  259.     dc_next->len = min(len, 255);
  260.     dc_next->prefix = prev_code;
  261.     if_debug3('w', "[w]decoding anomalous 0x%x=0x%x+%c\n",
  262.           next_code, prev_code, dc_next->datum);
  263.     }
  264.     /* See if there is enough room for the code. */
  265. reset:
  266.     len = table[code].len;
  267.     if (len == 255) {        /* Check for special code (reset or end). */
  268.     /* We set their lengths to 255 to avoid doing */
  269.     /* an extra check in the normal case. */
  270.     if (code == code_reset) {
  271.         if_debug1('w', "[w]reset: next_code was %d\n",
  272.               next_code);
  273.         next_code = code_0;
  274.         dc_next = table + code_0;
  275.         code_size = ss->InitialCodeLength + 1;
  276.         set_code_size();
  277.         prev_code = -1;
  278.         goto top;
  279.     } else if (code == eod) {
  280.         status = EOFC;
  281.         goto out;
  282.     }
  283.     /* The code length won't fit in a byte, */
  284.     /* compute it the hard way. */
  285.     for (c = code, len = 0; c != eod; len++)
  286.         c = table[c].prefix;
  287.     if_debug2('w', "[w]long code %d, length=%d\n", code, len);
  288.     }
  289.     if (wlimit - q < len) {
  290.     ss->copy_code = code;
  291.     ss->copy_left = ss->copy_len = len;
  292.     status = 1;
  293.     goto out;
  294.     }
  295.     /* Copy the string to the buffer (back to front). */
  296.     /* Optimize for short codes, which are the most frequent. */
  297.     dc = &table[code];
  298.     switch (len) {
  299.     default:
  300.         {
  301.         byte *q1 = q += len;
  302.  
  303.         c = code;
  304.         do {
  305.             *q1-- = (dc = &table[c])->datum;
  306.         }
  307.         while ((c = dc->prefix) != eod);
  308.         b = q1[1];
  309.         }
  310.         break;
  311.     case 3:
  312.         q[3] = dc->datum;
  313.         dc = &table[dc->prefix];
  314.     case 2:
  315.         q[2] = dc->datum;
  316.         dc = &table[dc->prefix];
  317.     case 1:
  318.         q[1] = b = dc->datum;
  319.         q += len;
  320.     }
  321.   add:                /* Add a new entry to the table */
  322.     if (prev_code >= 0) {
  323.     /*
  324.      * Unfortunately, we have to check for next_code ==
  325.      * lzw_decode_max every time: just checking at power
  326.      * of 2 boundaries stops us one code too soon.
  327.      */
  328.     if (next_code == lzw_decode_max) {
  329.         /*
  330.          * A few anomalous files have one data item too many before the
  331.          * reset code.  We think this is a bug in the application that
  332.          * produced the files, but Acrobat accepts the files, so we do
  333.          * too.
  334.          */
  335.         if (!ss->BlockData) { /* don't do this for GIF */
  336.         if (bits_left < 8 && p >= rlimit && last) {
  337.             /* We're at EOD. */
  338.             goto out;
  339.         }
  340.         if (bits_left + ((rlimit - p) << 3) < code_size) {
  341.             /*
  342.              * We need more data to decide whether a reset is next.
  343.              ****** PUNT ******
  344.              */
  345.             status = ERRC;
  346.             goto out;
  347.         }
  348.         if (low_order) {
  349.             code = bits >> (8 - bits_left);
  350.             code += (bits = *++p) << bits_left;
  351.             if (bits_left + 8 < code_size)
  352.             code += (bits = *++p) << (bits_left + 8);
  353.         } else {
  354.             code = bits & ((1 << bits_left) - 1);
  355.             code = (code << 8) + (bits = *++p);
  356.             if (bits_left + 8 < code_size)
  357.             code = (code << 8) + (bits = *++p);
  358.             code >>= (bits_left - code_size) & 7;
  359.         }
  360.         bits_left = (bits_left - code_size) & 7;
  361.         if (code == code_reset)
  362.             goto reset;
  363.         }
  364.         status = ERRC;
  365.         goto out;
  366.     }
  367.     dc_next->datum = b;    /* added char of string */
  368.     dc_next->len = min(prev_len, 254) + 1;
  369.     dc_next->prefix = prev_code;
  370.     dc_next++;
  371.     if_debug4('W', "[W]adding 0x%x=0x%x+%c(%d)\n",
  372.           next_code, prev_code, b, min(len, 255));
  373.     if (++next_code == switch_code) {    /* Crossed a power of 2. */
  374.         /* We have to make a strange special check for */
  375.         /* reaching the end of the code space. */
  376.         if (next_code < lzw_decode_max - 1) {
  377.         code_size++;
  378.         set_code_size();
  379.         if_debug2('w', "[w]crossed power of 2: new code_size=%d, next_code=%d\n",
  380.               code_size, next_code);
  381.         }
  382.     }
  383.     }
  384.     prev_code = code;
  385.     prev_len = len;
  386.     goto top;
  387.   out:pr->ptr = p;
  388.     pw->ptr = q;
  389.     ss->code_size = code_size;
  390.     ss->prev_code = prev_code;
  391.     ss->prev_len = prev_len;
  392.     ss->bits = bits;
  393.     ss->bits_left = bits_left;
  394.     ss->bytes_left = bytes_left;
  395.     ss->next_code = next_code;
  396.     if_debug3('w', "[w]decoded %d bytes, prev_code=%d, next_code=%d\n",
  397.           (int)(q - q0), prev_code, next_code);
  398.     return status;
  399. }
  400.  
  401. /* Stream template */
  402. const stream_template s_LZWD_template =
  403. {&st_LZW_state, s_LZWD_init, s_LZWD_process, 3, 1, s_LZW_release,
  404.  s_LZW_set_defaults, s_LZWD_reset
  405. };
  406.